// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

#include "GRIDParser.h"

#include <algorithm>
#include <cstdint>

#include "Utility/Logging/SGLogger.h"

#include "DataStructure/Input/Constraint/GRDSETData.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Node/INodeService.h"

#include "Import/BDFImport/BDFParserBase.h"
BULKDATA_ENTRY_PARSER_REG (GRID);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;

SG_IMPORT_BDF_NAMESPACE_OPEN
// GRDSET默认值
extern SG::DataStructure::FEM::GRDSETData* g_grdset;

SG_IMPORT_BDF_NAMESPACE_CLOSE

/*
// file://./../../Doc/MarkDown/BulkData/GRID/GRID.md>
*/
// 以下需要修改
enum GridKey : std::uint8_t
{
    GRIDKEY_ID   = 2,
    GRIDKEY_CP   = 3,  // 节点定义坐标系,默认为0
    GRIDKEY_X    = 4,
    GRIDKEY_Y    = 5,
    GRIDKEY_Z    = 6,
    GRIDKEY_CD   = 7,  // 节点位移输出坐标系
    GRIDKEY_PS   = 8,  // 节点上的约束
    GRIDKEY_SEID = 9,  // 超单元识别号
};

SG::DataStructure::Common::Status BDF::BULKDATA::ParseGRID (const std::shared_ptr<DBServiceFactory>& dbServiceFactory,
                                                            std::vector<std::string>&                dividedEntryInfo)
{
    // if (g_grdset != nullptr)
    // {
    //     SGInfo << "GRDSET is vaild! PS:" << g_grdset->m_PS;
    // }
    // else
    // {
    //     SGInfo << "GRDSET is not vaild!";
    // }

    // 创建临时节点数据结构，默认值为0
    SG::DataStructure::FEM::NodeData tempNode;

    // TODO(bug 001) m_dominId的正确填写
    tempNode.m_dominId = 1;

    int curIDKey = 1;  // 当前字符串是第几项

    // 遍历分割后的每个词条
    for (std::string& curStrKey : dividedEntryInfo)
    {
        // 检查当前字符串是否为空白
        bool const isEmpty = is_all_space (curStrKey);

        // 如果当前字符串为空白，则跳过并处理下一个
        if (isEmpty)
        {
            // 对空字段进行赋GRDSET的值
            if (g_grdset != nullptr)
            {
                switch (curIDKey)
                {
                case GRIDKEY_CP:
                    tempNode.m_defCoord = g_grdset->m_CP;
                    break;
                case GRIDKEY_CD:    // 节点位移输出坐标系
                    tempNode.m_dispCoord = g_grdset->m_CD;
                    break;
                case GRIDKEY_PS:    // 节点上的约束
                    tempNode.m_ps = g_grdset->m_PS;
                    break;
                case GRIDKEY_SEID:  // 超单元识别号
                    tempNode.m_seid = g_grdset->m_SEID;
                    break;
                default:            // 其他情况不做处理
                    break;
                }
            }
            curIDKey++;
            continue;
        }
        // 根据当前词条的位置进行相应的解析
        switch (curIDKey)
        {
        case GRIDKEY_ID:    // 节点ID
            tempNode.m_id = convertToI64 (curStrKey);
            break;
        case GRIDKEY_CP:    // 节点定义坐标系，默认为0
            tempNode.m_defCoord = convertToI64 (curStrKey);
            break;
        case GRIDKEY_X:     // X坐标
            tempNode.m_x[0] = convertToReal (curStrKey);
            break;
        case GRIDKEY_Y:     // Y坐标
            tempNode.m_x[1] = convertToReal (curStrKey);
            break;
        case GRIDKEY_Z:     // Z坐标
            tempNode.m_x[2] = convertToReal (curStrKey);
            break;
        case GRIDKEY_CD:    // 节点位移输出坐标系
            tempNode.m_dispCoord = convertToI64 (curStrKey);
            break;
        case GRIDKEY_PS:    // 节点上的约束
            tempNode.m_ps = convertToI64 (curStrKey);
            break;
        case GRIDKEY_SEID:  // 超单元识别号
            tempNode.m_seid = convertToI64 (curStrKey);
            break;
        default:            // 其他情况不做处理
            break;
        }

        // 处理完当前词条后，移动到下一个词条
        curIDKey++;
    }

    // 将解析完成的节点数据添加到数据库服务中
    return dbServiceFactory->get<INodeService> ()->Add (tempNode);
}